package sg.techstudio.suntec.controller;

import java.text.ParseException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import sg.techstudio.suntec.dao.RadiusDao;
import sg.techstudio.suntec.object.AjaxListResponse;
import sg.techstudio.suntec.object.Radacct;
import sg.techstudio.suntec.util.CustomTagUtil;

@Controller
public class CmsSessionController extends BaseController {
	private static final Log log = LogFactory.getLog(CmsSessionController.class);
	private static final String PARAM_FILTERDATESTART = "filterdatestart";
	private static final String PARAM_FILTERDATEEND = "filterdateend";
	
	@Override
	protected String getModuleFolder() {
		return "cms/session/";
	}
	
	@Autowired
	private RadiusDao radiusDao;

	@RequestMapping(value = "cms/session/list" + SPRING_EXT)
	@PreAuthorize("hasRole('ROLE_CMS')")
	public String session_list(Model model) {
		
		Calendar now = DateUtils.truncate(Calendar.getInstance(), Calendar.DATE);
		model.addAttribute(PARAM_FILTERDATEEND, SDF_DD_MM_YYYY.format(now.getTime()));
		now.add(Calendar.WEEK_OF_MONTH, -1);
		model.addAttribute(PARAM_FILTERDATESTART, SDF_DD_MM_YYYY.format(now.getTime()));
		
		return goToPageJsp("list");
	}
	
	@ResponseBody
	@RequestMapping(value = "cms/session/ajaxlist" + SPRING_EXT, headers="Accept=application/json")
	@PreAuthorize("hasRole('ROLE_CMS')")
	public AjaxListResponse session_ajaxlist(Model model
			, String sEcho
			, int iDisplayStart
			, int iDisplayLength
			, String sSearch
			, String sSearch_1
			, String sSearch_2
			, String sSearch_3
			, String sSearch_4
			, String sSearch_5
			, HttpServletRequest req, HttpServletResponse rep, HttpSession ses) {
		log.debug("session_ajaxlist() entry");
		
		Radacct filter = new Radacct();
		filter.setUsername(sSearch_1);
		filter.setFramedipaddress(sSearch_2);
		filter.setCallingstationid(sSearch_3);
		if(!StringUtils.isBlank(sSearch_4)) {
			try {
				filter.setAcctstarttime(SDF_DD_MM_YYYY.parse(sSearch_4));
			} catch (ParseException e) {
				log.error(e.getMessage(), e);
			}
		}
		if(!StringUtils.isBlank(sSearch_5)) {
			try {
				filter.setAcctstoptime(SDF_DD_MM_YYYY.parse(sSearch_5));
			} catch (ParseException e) {
				log.error(e.getMessage(), e);
			}
		}
		long recordNo = radiusDao.countRadacctList(sSearch, filter);
		AjaxListResponse resp = new AjaxListResponse();
		resp.setsEcho(sEcho);
		resp.setiTotalRecords(recordNo);
		resp.setiTotalDisplayRecords(recordNo);

		List<Radacct> list = radiusDao.getRadacctList(sSearch, filter, iDisplayStart, iDisplayLength);
		List<Map<String, Object>> aaList = new LinkedList<Map<String,Object>>();
		for(Radacct r: list){
			Map<String, Object> m = new HashMap<String, Object>();
			m.put("0", formatDateColumn(r.getAcctstarttime()));
			m.put("1", r.getUsername());
			m.put("2", r.getFramedipaddress());
			m.put("3", r.getCallingstationid());
			m.put("4", CustomTagUtil.humanReadableByteCount(r.getAcctinputoctets()));
			m.put("5", CustomTagUtil.humanReadableByteCount(r.getAcctoutputoctets()));
			m.put("DT_RowId", r.getRadacctid());
			aaList.add(m);			
		}
		resp.setAaData(aaList);
		log.debug("session_ajaxlist() exit");
		return resp;
	}
}
